package org.esgi.ppa.HUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.JFileChooser;

import org.esgi.ppa.controller.TestJob;
import org.esgi.ppa.controller.TestManager;
import org.esgi.ppa.controller.UnitTest;


public class ControlButtonForMainWindow implements ActionListener, KeyListener, PropertyChangeListener{
    MainWindow mw = null;

    public ControlButtonForMainWindow(MainWindow mw){
        this.mw=mw;
    }

    public void actionPerformed(ActionEvent e) {
        //Action performed on the add_test button
        if(e.getSource()==mw.add_test){
        	mw.showAddTestWindow();
        }
        else if(e.getSource() == mw.bSave){
        	JFileChooser fc = new JFileChooser();
        	
        	@SuppressWarnings("unused")
			int val = fc.showSaveDialog(mw);
        	File file = fc.getSelectedFile();
        	String path = ((-1 != file.getName().indexOf('.'))?file.getAbsolutePath().substring(0, file.getAbsolutePath().indexOf('.')):file.getAbsolutePath())+".tst";

        	mw.manager.save(new File(path));
        }
        else if(e.getSource() == mw.miOpen){
        	JFileChooser fc = new JFileChooser();
        	
        	@SuppressWarnings("unused")
			int val = fc.showOpenDialog(mw);
        	File file = fc.getSelectedFile();
        	if(file.getAbsolutePath().endsWith(".tst")){
        		mw.manager.open(file);
        	}
        }
        else if (e.getSource()==mw.bRun) {
        	
        	for(UnitTest unit : TestManager.tests){
        		for (int i = 0; i < unit.getRequests(); i++) {
        			TestJob tj=new TestJob(unit);
        			tj.run();
				}
        	}
		}
    }

    public void keyTyped(KeyEvent e) {

    }

    public void keyPressed(KeyEvent e) {

    }

    public void keyReleased(KeyEvent e) {

    }

    public void propertyChange(PropertyChangeEvent evt) {

    }

}
